package com.exp.practice.workbench.service.student.impl;

import com.exp.practice.base.exception.MyException;
import com.exp.practice.base.utils.DateTimeUtil;
import com.exp.practice.workbench.dao.*;
import com.exp.practice.workbench.domain.*;
import com.exp.practice.workbench.service.student.StudentPracticeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentPracticeServiceImpl implements StudentPracticeService {

    @Resource
    private ExaminationStudentRelationDao examinationStudentRelationDao;

    @Resource
    private ExaminationDao examinationDao;

    @Resource
    private QuestionDao questionDao;

    @Resource
    private ExaminationQuestionRelationDao examinationQuestionRelationDao;

    @Resource
    private ExaminationSituationDao examinationSituationDao;

    @Resource
    private ConcreteExaminationSituationDao concreteExaminationSituationDao;


    @Override
    public int getTotal(String stuId,String finished) {
        int total=examinationStudentRelationDao.getTotal(stuId,finished);
        return total;
    }

    @Override
    public List<Examination> getExaminationList(String pageNo, String pageSize,String stuId,String finished) {
        List<Examination> list=new ArrayList<>();
        int pageNos=Integer.valueOf(pageNo);
        int pageSizes=Integer.valueOf(pageSize);
        //记录略过的记录条数
        int skipCount=(pageNos-1)*pageSizes;
        String[] eids=examinationStudentRelationDao.getEids(stuId,finished);
        //全部已完成
        if(eids.length==0){
            return list;
        }
         list=examinationDao.getExaminationLists(skipCount,pageSizes,eids);
        return list;
    }

    @Override
    public Map<String, Object> getConcreteExamination(String eid) {
        Map<String,Object> map=new HashMap<>();
        //获得试卷记录
        Examination examination=examinationDao.getExaminationById(eid);
        //获得题目记录
        String[]qids=examinationQuestionRelationDao.getIds(eid);

        List<Question> list=questionDao.getQuestions(0,qids.length,qids);
        //将结果加入map中
        map.put("examination",examination);
        map.put("questionList",list);
        return map;
    }

    @Override
    public ExaminationSituation markTest(String stuId, String eid, int score) throws MyException {
        //添加一条答题情况记录
        String finishTime= DateTimeUtil.getDate();
        ExaminationSituation examinationSituation=new ExaminationSituation();
        examinationSituation.setEid(eid);
        examinationSituation.setStudentId(stuId);
        examinationSituation.setScore(score);
        examinationSituation.setFinishTime(finishTime);
        int count=examinationSituationDao.add(examinationSituation);
        String id=examinationSituation.getId();
        if(count!=1){
            throw new MyException("交卷失败，请重新答卷");
        }
        ExaminationSituation examinationSituation1=examinationSituationDao.getById(id);
        return examinationSituation1;
    }

    @Override
    public void insertConcreteExaminationSituation(ConcreteExaminationSituation concreteExaminationSituation) throws MyException {
        int count=concreteExaminationSituationDao.insert(concreteExaminationSituation);
        if(count!=1){
            throw new MyException("交卷失败，请重新答卷");
        }
    }

    @Override
    public void setFinishedStatus(String eid, String stuId) throws MyException {
        int count=examinationStudentRelationDao.setFinishedStatus(eid,stuId);
        if(count!=1){
            throw new MyException("交卷失败，请重新答卷");
        }
    }

    @Override
    public ExaminationSituation getExaminationSituation(String eid, String stuId) {
        ExaminationSituation examinationSituation=examinationSituationDao.getByEidAndStuId(eid,stuId);
        return examinationSituation;
    }

    @Override
    public List<ConcreteExaminationSituation> getBySituationId(String situationId) {
        List<ConcreteExaminationSituation> concreteExaminationSituationList=concreteExaminationSituationDao.getBySituationId(situationId);
        return concreteExaminationSituationList;
    }

}
